<?php

use Drupal\shusy_store\Entity\ShusyStore;
use Drupal\shusy_store_coupon\Entity\ShusyStoreCoupon;
use Drupal\shusy_store_coupon\Entity\ShusyStoreCouponHold;

function shusy_store_coupon_shusy_store_coupon_entity_form($form, &$form_state, ShusyStoreCoupon $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_store_coupon') . '/shusy_store_coupon.forms.inc';
  $form_state[ShusyStoreCoupon::class] = $e;

  $options = [];
  $stores = shusy_store_load_multiple(FALSE);
  foreach ($stores as $store) {
    if ($store instanceof ShusyStore) {
      $options[$store->id] = $store->label();
    }
  }
  $form['store_id'] = [
    '#type' => 'select',
    '#title' => t('所属店铺'),
    '#options' => $options,
    '#default_value' => $e->store_id,
    '#chosen' => TRUE,
    '#required' => TRUE,
  ];
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => t('标题'),
    '#default_value' => $e->title,
  ];
  $form['amount'] = [
    '#type' => 'textfield',
    '#title' => t('面额'),
    '#default_value' => $e->amount / 100,
    '#required' => TRUE,
  ];
  $form['condition_min'] = [
    '#type' => 'textfield',
    '#title' => t('条件 最小订单金额'),
    '#default_value' => $e->condition_min / 100,
    '#required' => TRUE,
  ];
  $form['status'] = [
    '#type' => 'checkbox',
    '#title' => t('启用'),
    '#default_value' => $e->status,
  ];
  $form['days'] = [
    '#type' => 'textfield',
    '#title' => t('领取后有效期天'),
    '#default_value' => $e->days,
    '#required' => TRUE,
  ];
  $form['is_validity'] = [
    '#type' => 'checkbox',
    '#title' => t('固定有效期'),
    '#default_value' => $e->is_validity,
  ];
  $form['validity_start'] = [
    '#type' => 'date_popup',
    '#title' => t('有效期开始'),
    '#date_format' => 'Y-m-d',
    '#date_label_position' => 'within',
    '#date_year_range' => '-1:+5',
    '#required' => TRUE,
    '#default_value' => format_date($e->validity_start, 'custom', 'Y-m-d'),
  ];
  $form['validity_end'] = [
    '#type' => 'date_popup',
    '#title' => t('有效期结束'),
    '#date_format' => 'Y-m-d',
    '#date_label_position' => 'within',
    '#date_year_range' => '-1:+5',
    '#required' => TRUE,
    '#default_value' => format_date($e->validity_end, 'custom', 'Y-m-d'),
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_store_coupon_shusy_store_coupon_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyStoreCoupon::class];
  if ($e instanceof ShusyStoreCoupon) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->amount = $e->amount * 100;
    $e->condition_min = $e->condition_min * 100;
    $e->validity_start = strtotime($e->validity_start);
    $e->validity_end = strtotime($e->validity_end);
    $e->save();
  }
}

function shusy_store_coupon_shusy_store_coupon_delete_form($form, &$form_state, ShusyStoreCoupon $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_store_coupon') . '/shusy_store_coupon.forms.inc';
  $form_state[ShusyStoreCoupon::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_store_coupon/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_store_coupon_shusy_store_coupon_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyStoreCoupon::class];
  if ($e instanceof ShusyStoreCoupon) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_store_coupon';
}

function shusy_store_coupon_shusy_store_coupon_hold_entity_form($form, &$form_state, ShusyStoreCouponHold $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_store_coupon') . '/shusy_store_coupon.forms.inc';
  $form_state[ShusyStoreCouponHold::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_store_coupon_shusy_store_coupon_hold_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyStoreCouponHold::class];
  if ($e instanceof ShusyStoreCouponHold) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_store_coupon_shusy_store_coupon_hold_delete_form($form, &$form_state, ShusyStoreCouponHold $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_store_coupon') . '/shusy_store_coupon.forms.inc';
  $form_state[ShusyStoreCouponHold::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_store_coupon_hold/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_store_coupon_shusy_store_coupon_hold_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyStoreCouponHold::class];
  if ($e instanceof ShusyStoreCouponHold) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_store_coupon_hold';
}

